<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/24
 * Time: 13:58
 */

namespace app\ext\controller;


use OSS\OssClient;
use think\Controller;
use think\Loader;

class Upload extends Controller
{
    public function index()
    {
        $this->assign('title','upload');
        return view('index');
    }

    public function uType()
    {
        $type = input('up_type')?:0;

        switch ($type)
        {
            case 0:
                $this->upload();break;
            case 1:
                $this->tp5Upload();break;
            case 2:
                $this->curlUpload();break;
            case 3:
                $this->OssUpload();break;
            case 4:
                $this->QiniuUpload();break;
        }
    }

    public function upload()
    {
        $file = $_FILES['file'];
        $dir_path = ROOT_PATH . 'public' . DS . 'uploads' . DS . date('Ymd') . DS;

        foreach($file['name'] as $key => $value)
        {
            $file_info = pathinfo($value);
            $save_name = time() . '_' . $key . '.' . $file_info['extension'];

            if(!file_exists($dir_path . $save_name)){
                $res = move_uploaded_file($file['tmp_name'][$key], $dir_path . $save_name);
            }

            $file[] = $dir_path . $save_name;
        }
        $this->success($file);
    }

    public function tp5Upload()
    {
        $file = request()->file('file');
        foreach($file as $vo)
        {
            $info = $vo->validate(['size'=>204800,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $info->getExtension();//文件后缀
                $info->getFilename();
                $pic[] = $info->getSaveName();
            }else{
                $error[] = $info->getError();
            }
        }
        if(empty($error)){
            $this->success($pic);
        }else{
            $this->error($error);
        }
    }

    public function curlUpload()
    {
        //curl把数据提交到其他服务器，其他服务器正常保存。
    }

    public function OssUpload()
    {
        $file = $_FILES['file'];
        $dir_path = ROOT_PATH . 'public' . DS . 'uploads' . DS . date('Ymd') . DS;

        foreach($file['name'] as $key => $value)
        {
            $file_info = pathinfo($value);
            $file_name = date('Ymd') . $key . '.' . $file_info['extension'];
            if(is_uploaded_file($file['tmp_name'][$key])){
                $pic[] = $this->saveToAliyun($file['tmp_name'][$key],$dir_path,$file_name);
            }else{
                $this->error('上传文件非法');
            }
        }
    }

    public function QiniuUpload()
    {
        $file = $_FILES['file'];
        $dir_path = ROOT_PATH . 'public' . DS . 'uploads' . DS . date('Ymd') . DS;

        foreach($file['name'] as $key => $value)
        {
            $file_info = pathinfo($value);
            $file_name = date('Ymd') . $key . '.' . $file_info['extension'];
            if(is_uploaded_file($file['tmp_name'][$key])){
                $pic[] = $this->seveToQiniu($file['tmp_name'][$key], $dir_path, $file_name);
            }else{
                $this->error('上传文件非法');
            }
        }
    }

    public function saveToAliyun($file, $path, $name)
    {
        Loader::import('Oss.autoload',EXTEND_PATH, '.php');
        //或者 直接引入文件require_once '/../extend/Oss/autoload.php';

        $config = [
            'access_key'        =>  '',
            'secret_key'        =>  '',
            'domain'            =>  '',
            'CNAME'             =>  '0'//是否对bucket做域名绑定，
        ];
        if($config['CNAME'] == 0){
            $ossClient = new OssClient($config['access_key'],$config['secret_key'],$config['domain']);
        }else{
            $ossClient = new OssClient($config['access_key'],$config['secret_key'],$config['domain'],true);
        }

        $res = $ossClient->uploadFile($config['bucket'],$path.$name,$file);

        file_exists($file) && unlink($file);
        if($config['CNAME'] == 0){
            $arr = explode('//',$config['domain']);
            $url = $arr[0].'//'.$config['bucket'].'.'.$arr[1] . '/' . $path.$name;
        }else{
            $url = $config['domain'].'/'.$path.$name;
        }

        if (!empty($res)) return $url;
        else return false;
    }

    public function seveToQiniu($file, $path, $name)
    {
        Loader::import('Qiniu.autoload',EXTEND_PATH, '.php');
        //或者 直接引入文件require_once '/../extend/Oss/autoload.php';

        $config = [
            'access_key'        =>  '',
            'secret_key'        =>  '',
            'bucket'            =>  ''
        ];

        $accessKey = $config['access_key'];//getenv('QINIU_ACCESS_KEY');
        $secretKey = $config['secret_key'];//getenv('QINIU_SECRET_KEY');
        $bucket = $config['bucket'];//getenv('QINIU_TEST_BUCKET');
        $auth = new \Qiniu\Auth($accessKey, $secretKey);
        $token = $auth->uploadToken($bucket);
        $uploadMgr = new \Qiniu\Storage\UploadManager();

        list($ret, $err) = $uploadMgr->putFile($token, $path . $name, $file);

        file_exists($file) && unlink($file);
        $url = $config['domain'] . '/' . $path . $name;
        if ($err !== null) return false;
        else return $url;
    }
}